/*
 * cAnimationManager.cpp
 *
 *  Created on: 05.04.2012
 *      Author: e_krais
 */

#include "cAnimationManager.h"

namespace strator {

cAnimationManager::cAnimationManager() {

}

cAnimationManager::~cAnimationManager() {

}

cAnimation* cAnimationManager::CreateAnimation(cTexture* tex, int from, int to)
{
	return CreateAnimation(tex, GetTextureMapRects(tex, from, to));
}

cAnimation* cAnimationManager::CreateAnimation(cTexture* tex, RectVector frames)
{
	cAnimation* newAnim = new cAnimation(tex);
	newAnim->SetFrames(frames);
	newAnim->Reset();
	RegisterAnimation(newAnim);
	return newAnim;
}

void cAnimationManager::PauseAllAnimation() {
	for (size_t i = 0; i < m_Animations.size(); ++i)
	{
		m_Animations[i] -> Pause();
	}
}

void cAnimationManager::ResumeAllAnimation() {
	for (size_t i = 0; i < m_Animations.size(); ++i)
	{
		m_Animations[i] -> Resume();
	}
}

void cAnimationManager::ResetAllAnimation() {
	for (size_t i = 0; i < m_Animations.size(); ++i)
	{
		m_Animations[i] -> Reset();
	}
}

void cAnimationManager::DeleteAllAnimation() {
	for (size_t i = 0; i < m_Animations.size(); ++i)
	{
		delete m_Animations[i];
	}
	m_Animations.clear();
}

void cAnimationManager::UpdateAllAnimation() {
	for (size_t i = 0; i < m_Animations.size(); ++i)
	{
		m_Animations[i] -> Update();
	}
}


uint cAnimationManager::CreateTextureMap(cTexture* tex, int x, int y) {

	if (tex == NULL)
		return -1;

	RectVector frames;

	int cx = tex->GetWidth() / x;
	int cy = tex->GetHeight() / y;
	GLfloat rx = (GLfloat)x / (GLfloat)tex->GetWidth();
	GLfloat ry = (GLfloat)y / (GLfloat)tex->GetHeight();


	sRect* frame;
	for (int j=0; j<cy; ++j)
	{
		for (int i=0; i<cx; ++i)
		{
			frame = new sRect(); // = {(i*x)f, (j*y)f, (i*x)f, j*y};
			frame->Left 		= i*rx;
			frame->Top  		= j*ry;
			frame->Right 	= (i+1)*rx;
			frame->Bottom 	= (j+1)*ry;
			frames.push_back(frame);
		}
	}
	m_AnimationMaps[tex] = frames;
	return cy*cx;
}

uint cAnimationManager::CreateTextureMap2(cTexture* tex, int partXCount, int partYCount)
{
	return CreateTextureMap(tex, tex->GetWidth() / partXCount, tex->GetHeight() / partYCount);
}

RectVector cAnimationManager::GetTextureMap(cTexture* tex)
{
	return m_AnimationMaps[tex];
}

RectVector cAnimationManager::GetTextureMapRects(cTexture* tex, int from, int to)
{
	RectVector map = GetTextureMap(tex);
	RectVector result;
	for (int i = from; i<=to; ++i)
		result.push_back(map[i]);
	return result;
}

sRect* cAnimationManager::GetTextureMapRect(cTexture* tex, int num)
{
	return m_AnimationMaps[tex][num];
}

void cAnimationManager::DeleteMap(cTexture* tex)
{
	RectVector frames = m_AnimationMaps[tex];
	for (size_t i=0; i<frames.size(); ++i)
	{
		delete frames[i];
		frames[i] = NULL;
	}
	frames.clear();
}

void cAnimationManager::DeleteAllMaps()
{
	std::map<cTexture*, RectVector>::iterator it;
	for (it = m_AnimationMaps.begin(); it != m_AnimationMaps.end(); ++it)
	{
		DeleteMap((*it).first);
	}
	m_AnimationMaps.clear();
}

void cAnimationManager::RegisterAnimation(cAnimation* anim) {
	if (anim != NULL)
		m_Animations.push_back(anim);
}

void cAnimationManager::UnregsterAnimation(cAnimation* anim) {
	if (anim != NULL)
	{
		int place = -1;
		//lets find where the animation is in the vector
		for( uint i = 0; i < m_Animations.size(); i++)
			if( m_Animations[i] == anim )
				place = i;
		//if its not registed then do nothing
		if(place == -1)
			return;

		//if the place is at the end just remove the animation
		if( (place + 1) == (int)m_Animations.size())
		{
			m_Animations.erase( m_Animations.begin() + place);
		}
		else // move the last place to the empty slot and remove the last one element
		{
			m_Animations[place] = m_Animations[ m_Animations.size() - 1 ];
			m_Animations.erase( m_Animations.begin() + place);
		}
		delete anim;
	}
}

}
